Transitioning from a Display to an Input Form
To transition the application from a display to an input form and back, you will:
- Add an Add New Part option at the top of the ListBox.
- Hide the TextBox that displays the part type.
- Display the ComboBox and the PushButton when the user clicks the Add New Part option.
- Populate the ComboBox with all part types retrieved from the database.
Add a new option to a ListBox that uses bindFormatFunction().
Typically, to add an option to a ListBox you simply use the following
code:
parts_lb.addItemAt(0,"Add New Part",null);
This inserts the Add New Part text as the first (remember
the ListBox is zero-indexed) option in the box. There is no data associated
with this option. However, in this tutorial, you use the bindFormatFunction(),
complicating the insertion a bit more.
Remember, the binder function assigned the name property of a record
to the label part of what displayed. You will add Add New Part to the
ListBox when using bindFormatFunction() using the following
steps:
- Create a temporary object of the Object class.
- Assign a name property with the value Add New Part to the object.
- Add the object to the RecordSet.
- Use
bindFormatFunction()as you did before.
Using bindFormatStrings(). The DataGlue
object also has the bindFormatStrings() method, in addition
to the bindFormatFunction() that you have already used. Using
bindFormatStrings() is easier, because you don’t have
to write any extra functions to use it. The general format is:
DataGlue.bindFormatStrings(dataConsumer, RecordSet, "#labelColumnName#", "#dataColumnName#");
Note: When filling the label and data portions, you
only use the string value from a single column of the RecordSet returned.
Earlier in the tutorial you used the bindFormatFunction()
instead of the bindFormatStrings(), because at that point,
you needed to store an entire record in the data portion.
Exercise 4
In this exercise you will learn how to add a new option to the top of the ListBox, retrieve part types from the database, display the results, and transition to the view interface.
-
Adding a New Option to the Top of the ListBox
- In devnet.fla, locate the partResponder.onResult method in the object methods section.
- As the first line of the code in the method, create a variable local to the function named tempObj. tempObj is an object of the Object class.
- In the method, set a name property of the new object equal to the text Add New Part.
- In the method, use the addItemAt() method to add the new object to the parts_rs RecordSet at the 0th position.
- To automatically highlight the first part instead of the newly added text, change the setSelectedIndex() argument to 1 instead of 0 in the method.
-
Ensure your function now appears as shown here:
partResponder.onResult=function(parts_rs){ var tempObj=new Object; tempObj.name="Add New Part"; parts_rs.addItemAt(0,tempObj); DataGlue.bindFormatFunction(parts_lb,parts_rs,binder); parts_lb.setSelectedIndex(1); } - Save the document and test the movie. You should now see that the first option in the ListBox is the text Add New Part. Also note that the app the first actual part name is selected and its details are displayed.
- Retrieving All Possible Part Types from the Database. When supplying
part data in the input form, users should only be able to choose certain
part types from a ComboBox. The part types in the ComboBox are retrieved
from the database.
-
Open the CFC in Dreamweaver MX. In this exercise, you will retrieve the part types for the ComboBox from the database. The CFC uses a <cfquery> tag and a SQL SELECT statement. The tag is shown here:
<cfquery name="qGetTypes" datasource="bikeparts"> SELECT partTypeID, partType FROM PartType ORDER BY partType ASC </cfquery>
-
In the object creation section, create an object named typeResponder using the following syntax:
var typeResponder=new Object();
-
In the run at once commands section, just below where you invoked the getPartInfo() method, invoke the getPartTypes() method, as follows:
partService.getPartTypes(typeResponder);
- At the top of the object methods section, create the skeleton for the onResult method attached to the object you just created. Use as the argument the variable types_rs.
-
In the method, assign a variable named partTypes_rs the variable that was used as the argument, types_rs.
Note: You need to reassign the variable because types_rs is by default local to the function and would not have meaning outside of the function. You will need to use the RecordSet returned in another location on the page, so you are simply creating a variable you can use in other locations.
-
Check to make sure your method appears as follows:
typeResponder.onResult=function(types_rs){ partTypes_rs=types_rs; }
-
-
Writing the Method to Transition the Display into an Input Form
-
At the top of the object methods section, create a skeleton for an
addPartmethod attached to theparts_lbobject, as shown here:parts_lb.addPart=function(){ } - In the function, use the
_visible propertyof thetype_txtobject to hide it. - In the method, use the
_visible propertyto make both thetypes_cbandaddPart_pbobjects visible. -
In the method, clear any data in the following TextBoxes by setting the text property equal to the empty string:
- name_txt
- type_txt
- price_txt
- desc_txt
-
In the method, use the
bindFormatStrings()method to populate the ComboBoxtypes_cb. Users should be able to see the partType and assign the partTypeID to the data. Use the following values:- dataConsumer:
types_cb - dataProvider:
partTypes_rs - labelString:
"#partType#" - dataString:
"#partTypeID#"
- dataConsumer:
-
Ensure your method appears as shown here:
parts_lb.addPart=function(){ type_txt._visible=false; types_cb._visible=true; addPart_pb._visible=true; name_txt.text=""; type_txt.text=""; price_txt.text=""; desc_txt.text=""; DataGlue.bindFormatStrings(types_cb,partTypes_rs, "#partType#","#partTypeID#"); }
-
-
Transition back to the display
- In the object methods section, locate the parts_lb.fillTextBoxes method.
-
At the top of the method, set the _visible property of the following objects to the value shown:
- type_txt: true
- types_cb: false
- addParts_pb: false
-
Check to be sure your method appears as shown here:
parts_lb.fillTextBoxes=function(){ type_txt._visible=true; types_cb._visible=false; addPart_pb._visible=false; name_txt.text=parts_lb.getSelectedItem().data.name; type_txt.text=parts_lb.getSelectedItem().data.partType; price_txt.text=parts_lb.getSelectedItem().data.price; desc_txt.text=parts_lb.getSelectedItem().data.description; }
-
Invoke the correct method. You now have two different methods that you must call based on what the user selects in the ListBox. When the user clicks the text Add New Part, invoke the
addPartmethod, and when the user clicks any actual part, invoke thefillTextBoxesmethod. Write a simple method to invoke the correct method using the following steps:-
At the top of the object methods section, create a skeleton for a displayOrAdd method attached to the parts_lb object, as follows:
parts_lb.displayOrAdd=function(){ } -
In the method, use an if statement that checks the following criteria:
- If the text Add New Part was clicked in the ListBox.
- If the condition is true, it invokes the
parts_lb.AddPart()method. - If the condition is false, it invokes the
parts_lb.fillTextBoxes()method.
-
Ensure your method appears as shown here:
parts_lb.displayOrAdd=function(){ if (parts_lb.getSelectedIndex() == 0){ parts_lb.addPart(); }else{ parts_lb.fillTextBoxes(); } } -
In the event handler assignment section, change the
setChangeHandlerforparts_lbto invokedisplayOrAddinstead offillTextBoxes, as follows:parts_lb.setChangeHandler("displayOrAdd",parts_lb); - Save the document and test the movie. When you click a part in the ListBox, the details display as they did before this exercise. If you click Add New Part, the dynamically populated ComboBox appears along with the PushButton. If you click a part again, the part details should appear.
-
Submit feedback on our tutorials, articles, and sample applications.